﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using StarsAdmin.Core.DB.Entities;

namespace StarsAdmin.Core.DB.Configs
{
    public class OperationLogConfig : IEntityTypeConfiguration<OperationLogEntity>
    {
        public void Configure(EntityTypeBuilder<OperationLogEntity> builder)
        {
            builder.ToTable("T_OprationLogs");
            // 一对多配置，并禁止级联删除
            builder.HasOne(e => e.User).WithMany().HasForeignKey(e => e.UserId).OnDelete(DeleteBehavior.Restrict);
            builder.Property(p => p.ApiLabel).IsRequired(false).HasMaxLength(64);
            builder.Property(p => p.ApiPath).IsRequired().HasMaxLength(256);
            // 为 ApiPath 字段添加非聚集索引
            builder.HasIndex(e => e.ApiPath).HasDatabaseName("IX_OprationLogs_ApiPath").IsClustered(false);
            builder.Property(p => p.HttpMethod).IsRequired().HasMaxLength(64);
            builder.Property(p => p.Ip).IsRequired().HasMaxLength(64);
            builder.Property(p => p.Browser).IsRequired(false).HasMaxLength(64);
            builder.Property(p => p.Os).IsRequired(false).HasMaxLength(64);
            builder.Property(p => p.Device).IsRequired(false).HasMaxLength(64);
            builder.Property(p => p.UserAgent).IsRequired().HasMaxLength(256);
            builder.Property(p => p.Params).IsRequired(false);
            builder.Property(p => p.Message).IsRequired(false);
            builder.Property(p => p.Result).IsRequired(false);
            //日志不需要LastUpdateDateTime属性，所以设置忽略LastUpdateDateTime属性
            builder.Ignore(p => p.LastUpdateDateTime);
        }
    }
}